package cn.tedu.medicalappointmentsystemk.controller;

import cn.tedu.medicalappointmentsystemk.pojo.bo.DoctorSelectBO;
import cn.tedu.medicalappointmentsystemk.pojo.bo.DoctorSelectByDoctorIdBO;
import cn.tedu.medicalappointmentsystemk.pojo.bo.DoctorSelectByIdBO;
import cn.tedu.medicalappointmentsystemk.pojo.bo.DoctorSelectTypeBO;
import cn.tedu.medicalappointmentsystemk.pojo.param.DoctorAddParam;
import cn.tedu.medicalappointmentsystemk.pojo.param.DoctorUpdateParam;
import cn.tedu.medicalappointmentsystemk.pojo.po.PageData;
import cn.tedu.medicalappointmentsystemk.response.JsonResult;
import cn.tedu.medicalappointmentsystemk.service.DoctorService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("v1/doctors")
@Api(tags = "医生模块")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    @PostMapping("add")
    @PreAuthorize("hasAuthority('/doctors/insert')")
    @ApiOperationSupport(order = 310)
    @ApiOperation("新增医生")
    public JsonResult addDoctor(DoctorAddParam doctorAddParam){
        log.info("开始执行新增医生功能,入参:{}",doctorAddParam);
        doctorService.addDoctor(doctorAddParam);
        log.info("执行新增医生功能结束");
        return JsonResult.ok();
    }
    @GetMapping("select/pageNum={pageNum}")
    @PreAuthorize("hasAuthority('/doctors/select')")
    @ApiOperationSupport(order = 320)
    @ApiOperation("查询全部医生信息(用于在系统界面显示)")
    public JsonResult selectDoctor(@PathVariable Integer pageNum){
        SecurityContext securityContext = SecurityContextHolder.getContext();
        log.info("开始执行查询全部医生信息功能:{}",securityContext);
        PageData<DoctorSelectBO> pageData = doctorService.selectDoctor(pageNum);
        log.info("执行查询全部医生信息功能结束,出参:{}",pageData);
        return JsonResult.ok(pageData);
    }
    @PostMapping("{id}/delete")
    @PreAuthorize("hasAuthority('/doctors/delete')")
    @ApiOperationSupport(order = 330)
    @ApiOperation("删除医生")
    public JsonResult deleteDoctor(@PathVariable Long id){
        log.info("开始执行依据医生id:[{}]删除医生功能",id);
        doctorService.deleteDoctor(id);
        log.info("执行删除医生功能结束");
        return JsonResult.ok();
    }
    @GetMapping("{id}/select")
    @PreAuthorize("hasAuthority('/doctors/select')")
    @ApiOperationSupport(order = 340)
    @ApiOperation("查询医生信息")
    public JsonResult selectDoctorById(@PathVariable Long id){
        log.info("开始执行依据医生id:[{}]查询医生信息功能",id);
        DoctorSelectByIdBO bo = doctorService.selectDoctorById(id);
        log.info("执行依据医生id:[{}]查询医生信息功能结束,出参:{}",id,bo);
        return JsonResult.ok(bo);
    }
    @PostMapping("change")
    @PreAuthorize("hasAuthority('/doctors/update')")
    @ApiOperationSupport(order = 350)
    @ApiOperation("修改医生信息")
    public JsonResult changeDoctor(DoctorUpdateParam param){
        log.info("开始执行修改医生信息功能,入参:{}" , param);
        doctorService.changeDoctor(param);
        log.info("执行修改医生信息功能结束");
        return JsonResult.ok();
    }
    @GetMapping("type")
    @PreAuthorize("hasAuthority('/doctors/select')")
    @ApiOperationSupport(order = 360)
    @ApiOperation("查询医生信息(用于选择界面)")
    public JsonResult doctorType(){
        log.info("开始执行用于选择界面的医生信息查询功能");
        List<DoctorSelectTypeBO> bo = doctorService.selectDoctorType();
        log.info("执行用于选择界面的医生信息查询功能结束,出参:{}",bo);
        return JsonResult.ok(bo);
    }
    @GetMapping("select/{id}")
    @PreAuthorize("hasAuthority('/doctors/select')")
    @ApiOperationSupport(order = 370)
    @ApiOperation("查询医生部分信息(用于排班界面)")
    public JsonResult doctorSelect(@PathVariable Long id){
        log.info("开始执行依据医生id:[{}]查询医生部分信息功能",id);
        DoctorSelectByDoctorIdBO bo = doctorService.selectDoctorByDoctorId(id);
        log.info("执行依据医生id:[{}]查询医生部分信息功能结束,出参:{}",id,bo);
        return JsonResult.ok(bo);
    }
    @GetMapping("selectByNickname/{nickname}")
    @PreAuthorize("hasAuthority('/doctors/select')")
    @ApiOperationSupport(order = 380)
    @ApiOperation("根据医生名字查询医生相关信息(用于搜索)")
    public JsonResult doctorSelectByNickname(@PathVariable String nickname){
        log.info("开始执行依据医生姓名:[{}]查询医生信息功能",nickname);
        List<DoctorSelectBO> doctorSelectBOS = doctorService.selectByNickname(nickname);
        log.info("执行依据医生姓名:[{}]查询医生信息功能结束,出参:{}",nickname,doctorSelectBOS);
        return JsonResult.ok(doctorSelectBOS);
    }
    @GetMapping("selectTotal")
    @PreAuthorize("hasAuthority('/doctors/select')")
    @ApiOperationSupport(order = 390)
    @ApiOperation("查询医生总数")
    public JsonResult doctorSelectTotal(){
        log.info("开始执行查询医生总数");
        Integer total = doctorService.selectTotal();
        log.info("执行查询医生总数结束,出参:{}",total);
        return JsonResult.ok(total);
    }
    @GetMapping("/searchToday/{pageNum}")
    @PreAuthorize("hasAnyAuthority('/departments/select')")
    @ApiOperationSupport(order = 4100)
    @ApiOperation("查询今日入职的医生")
    public JsonResult searchToday(@PathVariable Integer pageNum){
        PageData<DoctorSelectBO> pageData = doctorService.searchToday(pageNum);
        return JsonResult.ok(pageData);
    }
    @GetMapping("/searchLastWeek/{pageNum}")
    @PreAuthorize("hasAnyAuthority('/departments/select')")
    @ApiOperationSupport(order = 4200)
    @ApiOperation("查询7日内入职的医生")
    public JsonResult searchLastWeek(@PathVariable Integer pageNum){
        PageData<DoctorSelectBO> pageData = doctorService.searchLastWeek(pageNum);
        return JsonResult.ok(pageData);
    }
    @GetMapping("/searchLastMonth/{pageNum}")
    @PreAuthorize("hasAnyAuthority('/departments/select')")
    @ApiOperationSupport(order = 4300)
    @ApiOperation("查询30日内入职的医生")
    public JsonResult searchLastMonth(@PathVariable Integer pageNum){
        PageData<DoctorSelectBO> pageData = doctorService.searchLastMonth(pageNum);
        return JsonResult.ok(pageData);
    }

}
